package com.Business.web;

import com.Base.BaseServlet;
import com.Base.ResultCode;
import com.Business.service.*;
import com.Login.Service.LoginService;
import com.Pojo.Project;
import com.Pojo.User;
import com.alibaba.druid.util.StringUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by XingYang on 2019/3/13.
 */
@Controller
@RequestMapping(value = "business.spr")
public class BusinessController  extends BaseServlet{
    @Autowired
    ProjectService projectService;
    @Autowired
    LoginService loginService;
    @Autowired
    BusinessServiceImpl businessService;
    @Autowired
    BuildingService buildingService;
    @Autowired
    LandService landService;
    @RequestMapping(params = "method=createProject" ,method = RequestMethod.POST)
    @ResponseBody
    public String createProject(@RequestBody Project project,HttpServletRequest request){
        if(StringUtils.isEmpty(project.getProjectname())){
            return   sendResponse(ResultCode.NullArguments,"传入参数无效！",null);
        }
        project.setCreateop(((User)request.getAttribute("user")).getId());
        String proejctId = "";
        proejctId = projectService.AddProjectInfo(project);
        log.info(proejctId+"项目已录入");
        Map map = new HashMap();
        map.put("projectId",proejctId);
        return sendResponse(ResultCode.SUCCESS,"success",map);
    }

    @RequestMapping(params = "method=queryProject")
    @ResponseBody
    public String queryProject(@RequestParam String projectId, HttpServletRequest request){
        if(!StringUtils.isEmpty(projectId)){
            Map map = new HashedMap();
            map = projectService.getProjectInfByProjectNo(projectId);
            return sendResponse(ResultCode.SUCCESS,"success",map);
        }

        return sendResponse(ResultCode.Failed,"查询失败",null);
    }

    @RequestMapping(params = "method=getProjectList")
    @ResponseBody
    public String getProjectList(@RequestBody Map map,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        Subject currentUser = ThreadContext.getSubject();
        if(!map.containsKey("state")){
            return sendResponse(ResultCode.NullArguments,"参数缺失",null);
        }

        return sendPageResponse(ResultCode.SUCCESS,"",projectService.getProjectList(map,currentUser,user));
    }

    @RequestMapping(params = "method=getManagerList")
    @ResponseBody
    public String getManagerList(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        if(!StringUtils.isEmpty(user.getMainaccount())){
            return sendResponse(ResultCode.Failed,"只有主账号可进行此操作！",null);
        }

        Map map = new HashMap();
        map.put("managers",loginService.queryManagerByMainId(user.getId()));


        return sendResponse(ResultCode.SUCCESS,"",map);
    }
    @RequestMapping(params = "method=delProject")
    @ResponseBody
    public String delProject(@RequestParam String projectId, HttpServletRequest request){
        if(!StringUtils.isEmpty(projectId)){
            Map map = new HashedMap();
            map.put("projectNo",projectId);
            User user = (User) request.getSession().getAttribute("user");
            map.put("uid",user.getId());
            projectService.delProject(map);
            return sendResponse(ResultCode.SUCCESS,"success",map);
        }

        return sendResponse(ResultCode.Failed,"查询失败",null);
    }
    @RequestMapping(params = "method=getAttrs")
    @ResponseBody
    public String getAttrs(@RequestParam String param){
        Map map = new HashMap();
        map.put("attrs",businessService.getAttrs(param));
        return sendResponse(ResultCode.SUCCESS,"",map);
    }

    @RequestMapping(params = "method=getObjInf")
    @ResponseBody
    public String getBuilding(@RequestParam String objId){
        if(StringUtils.isEmpty(objId)){
            return sendResponse(ResultCode.NullArguments,"参数缺失",null);
        }
        Map map = new HashMap();
        Map obj = new HashMap();
        if("B".equals(objId.substring(objId.length()-5,objId.length()-4))){
            obj.putAll(buildingService.queryBuildingByBid(objId));
        }
        if ("L".equals(objId.substring(objId.length()-5,objId.length()-4))){
            obj.putAll(landService.getLandByLid(objId));
        }
        Map report = new HashMap();
        report = reportService.getReport(objId);
        if(report!=null){
        obj.putAll(report);
        }
        map.put("attrs",obj);
        return sendResponse(ResultCode.SUCCESS,"",map);
    }

    @RequestMapping(params = "method=updateObj",method = RequestMethod.POST)
    @ResponseBody
    public String updateObj(@RequestBody Map<String,String> map){
        String objId = map.get("objId");
        if(StringUtils.isEmpty(objId)){
            return sendResponse(ResultCode.NullArguments,"参数错误",null);
        }
        if(map.containsKey("step")){
            Map step = new HashMap();
            step.put("objId",objId);
            step.put("status",map.get("step"));
            projectService.changeObjStatus(step);
        }

        map.put("id",objId);
        if("B".equals(objId.substring(objId.length()-5,objId.length()-4))){
            buildingService.updateBuildingByBid(map);
        }
        if ("L".equals(objId.substring(objId.length()-5,objId.length()-4))){
            landService.updateLandByLid(map);
        }


        return sendResponse(ResultCode.SUCCESS,"success",null);
    }

    @Autowired
    ReportService reportService;

    @RequestMapping(params = "method=updateReport",method = RequestMethod.POST)
    @ResponseBody
    public String updateReport(@RequestBody Map<String,String> map){
        String objId = map.get("objId");
        if(StringUtils.isEmpty(objId)){
            return sendResponse(ResultCode.NullArguments,"参数错误  objId为空",null);
        }
        if(reportService.getReport(objId)==null){
            log.info("创建报告对象"+objId);
            reportService.addReport(map);
        }
        map.put("objid",objId);
        reportService.updateReport(map);

        return sendResponse(ResultCode.SUCCESS,"success",null);
    }




}
